(function ({form,table,layer,laydate,SelectDatas,staffTableIns,url,contentType,ok,AjaxFn,tablebtn,updataOpen,startAndForbid}) {

    SelectDatas = (function (data) {
        data = AjaxFn(url+"staffSelectDatas","",{});
        return data;
    })({}),

    (function ({positions,option='<option value=""></option>'}) {// 职位编号 vpositionid
        /*{vpositionid: "ZW0001", vpositionname: "1"}*/
        for(let i in positions){
            option+=`<option value="${positions[i].vpositionid}">${positions[i].vpositionname}</option>`;
        }
        $("select[name=vpositionid]").html(option);
    })(SelectDatas),
    (function ({teams,option='<option value=""></option>'}) {//班组编号 vteamid
        /*{vteamid: "BZ0002", vfactoryflg: "0", vteamname: "222", vappraiseflg: "0"}*/
        for(let i in teams){
            option+=`<option value="${teams[i].vteamid}">${teams[i].vteamname}</option>`;
        }
        $("select[name=vteamid]").html(option);
    })(SelectDatas),
    (function ({factorys,option='<option value=""></option>'}) {//工厂（类型：001）vfactoryflg
        /*0: {stype: "001", scode: "0", vname: "陆友"}*/
        for(let i in factorys){
            option+=`<option value="${factorys[i].scode}">${factorys[i].vname}</option>`;
        }
        $("select[name=vfactoryflg]").html(option);
    })(SelectDatas),
    (function ({appraises,option='<option value=""></option>'}) {//是否为评定组（类型：002）vappraiseflg*/
            /*{stype: "002", scode: "0", vname: "否"}*/
            for(let i in appraises){
                option+=`<option value="${appraises[i].scode}">${appraises[i].vname}</option>`;
            }
            $("select[name=vappraiseflg]").html(option);
        })(SelectDatas),

    form.render(),
    form.verify({
        myvphone:function (value, item) {
            if(value!=''){
                if (!/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(value)) return "手机号格式不正确";
            }

        }
    })
    staffTableIns=table.render({
            elem:'#staffTable',
            url:url+"staffTableDatas",
            method:'post',
            contentType,
            page:{first: '首页', last: '尾页', prev: '上一页', next: '下一页', layout: ['prev', 'page', 'next'], groups: 5, theme: '#1e9fff', limits:[10,20,30,40,50],},
            cols: [
                [
                    { title: '序号',type:'numbers',fixed:"left",unresize: true, align: "center",width:50, minWidth: 30},
                    { title: '操作',fixed:"left",unresize: true, align: "center", width:150,minWidth: 150,templet:function (d,updata = '',start='',forbid='') {
                            updata = tablebtn({"event":"staffUpdata", "btnclass":"layui-btn layui-btn-xs updata-btn-color", "icon":'&#xe642;', "text":'修改', "disabled":false});
                            forbid = d.vforbidden=='1'?
                            tablebtn({"event":"staffStart", "btnclass":"layui-btn layui-btn-xs start-btn-color", "icon":'&#xe683;', "text":'启用', "disabled":false}):
                            tablebtn({"event":"staffForbid", "btnclass":"layui-btn layui-btn-xs forbid-btn-color", "icon":'&#xe673;', "text":'禁用', "disabled":false});
                            return '<div>'+updata+forbid+'</div>';
                        }
                    },
                    {field: 'vforbiddenname', title: '状态', unresize: true, align: "center",width:80,minWidth: 80,},
                    {field: 'vstaffid', title: '员工编号', unresize: true, align: "center", minWidth: 100},
                    {field: 'vstaffname', title: '员工姓名', unresize: true, align: "center",width:120,minWidth: 120,},
                    {field: 'vphone', title: '手机号', unresize: true, align: "center", width:120,minWidth: 120,},
                    {field: 'vappraiseflgname', title: '评定组', unresize: true, align: "center", width:80,minWidth: 80,},
                    {field: 'vpositionname', title: '职位', unresize: true, align: "center", minWidth: 100},
                    {field: 'vfactoryflgname', title: '工厂', unresize: true, align: "center", width:100,minWidth: 100,},
                    {field: 'vteamname', title: '班组', unresize: true, align: "center", minWidth: 100},
                    {field: 'vinsdate', title: '创建时间', unresize: true, align: "center", width:120,minWidth: 120,}

                ]
            ]
        })

    $("#staffAdd").click(function () {
        updataOpen({url:url+"staffAddHtml",skin:"staff-Add",title:"员工新增",tableIns:staffTableIns,btns:['取消','新增']})
    })

    form.on("submit(staffSelectTable)",function ({field}) {
        staffTableIns.reload({
            where:field,
            page: {curr: 1}
        })
        return false;
    })

    table.on("tool(staffTable)",function ({data,event,tr}) {
        event=="staffUpdata"?(function(){
            updataOpen({url:url+"staffUpdataHtml/"+data.vstaffid,skin:"staffUpdata",title:"员工修改",tableIns:staffTableIns,btns:['取消','修改']})
        })():event=="staffStart"?(function(){
            startAndForbid({
                startAndForbid:'启用',
                masg:"确定启用该员工吗？",
                btns:['取消','启用'],
                param:data,
                tableIns:staffTableIns,
                url:'staffStartAndForbid'
            });
        })():event=="staffForbid"?(function(){
            startAndForbid({
                startAndForbid:'禁用',
                masg:"确定禁用该员工吗？",
                btns:['取消','禁用'],
                param:data,
                tableIns:staffTableIns,
                url:'staffStartAndForbid'
            });
        })():'';
    })

})({
    form: layui.form,
    table: layui.table,
    layer: layui.layer,
    laydate:layui.laydate,
    SelectDatas:{},
    staffTableIns:'',
    url: "/evaluation/staff/",
    contentType: 'application/json;charset=utf-8',
    ok:function(msg,icon){
        layui.layer.confirm(msg, {
            id: "ok",
            icon: icon,
            anim: 4,
            title: '提示',
            btn: ['确定'],
            closeBtn:false,
            success: function (layero) {
                var btn = layero[0].getElementsByClassName('layui-layer-btn')[0].getElementsByTagName('A')[0];
                btn.href = 'javascript:void(0)';
                btn.focus();
            },
            yes: function(index, layero){
                layer.close(index); //再执行关闭
            },
            end:function () {
                var index2 = parent.layer.getFrameIndex(window.name);
                parent.layer.closeAll();
            }
        });
    },
    AjaxFn:function (url,data,resource) {
        $.ajax({
            url,
            contentType:'application/json;charset=utf-8',
            type:'post',
            async:false,
            data,
            success:function(res) {
                let {msg='',code=''} =res;
                msg=='' && code==''? top.location.reload() : "";
                resource = res;
            }
        });
        return resource;
    },
    tablebtn:function({event="",btnclass="layui-btn btncolor ",icon="",text="",disabled=false,btn=""}){
        disabled?disabled="disabled":disabled='';
        btn=`<button type="button" ${disabled} class="${btnclass}" lay-event="${event}"><i class="layui-icon">${icon}</i>${text}</button>`
        return btn;
    },
    updataOpen:function ({url,skin,title,tableIns,btns=['取消','修改']}) {
        layer.open({
                anim: 4, type: 2, skin: skin,resize:false,
                title: [title, 'font-size:18px; background-color:#e7eaec;'],
                area: ['aotu'],
                btn: btns,
                content: url,
                btn2: function(index, layero){
                    var body = layer.getChildFrame('body', index);
                    body.find('#submit').click();
                    return false;
                },
                end: function () {
                    tableIns.reload();
                }
            }
        ),$(`.${skin} iframe`).css('height', '100%'),
            $(".layui-layer-btn").css({"padding-top":"0px","padding-bottom": "3px","background-color":"rgb(231, 234, 236)"});
    },
    startAndForbid:function ({startAndForbid,masg,btns,param,tableIns,url}) {
        layui.layer.confirm(masg, {
            id: "startAndForbid",
            icon: 3, anim: 4, title: '提示',
            btn: btns,
            success: function (layero) {
                let btn = layero[0].getElementsByClassName('layui-layer-btn')[0].getElementsByTagName('A')[0];btn.href = 'javascript:void(0)', btn.focus();},
            btn2: function(index, layero){
                let del_flg = param.vforbidden
                del_flg=="1"?param.vforbidden=0:del_flg=="0"?param.vforbidden=1:"";
                param.startAndForbid = startAndForbid,
                    $.ajax({
                        url:'/evaluation/staff/'+url,
                        contentType:'application/json;charset=utf-8',
                        type:'post',
                        async:false,
                        data:JSON.stringify(param),
                        success:function(res) {
                            let {msg='',code=''} =res;
                            msg=='' && code==''? top.location.reload() : code==200?icon=1:icon=5;
                            layer.msg(msg,{icon: icon});
                        },
                    })
            },
            end:function () {
                tableIns.reload();
            }
        });
    },
})